package com.work.yshop.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.work.yshop.R;
import com.work.yshop.model.Favorite;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 收藏适配器
 */
public class FavoriteAdapter extends RecyclerView.Adapter<FavoriteAdapter.FavoriteViewHolder> {
    
    private List<Favorite> favoriteList = new ArrayList<>();
    private OnItemClickListener listener;
    
    public void setData(List<Favorite> favoriteList) {
        this.favoriteList = favoriteList;
        notifyDataSetChanged();
    }
    
    @NonNull
    @Override
    public FavoriteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_favorite, parent, false);
        return new FavoriteViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull FavoriteViewHolder holder, int position) {
        Favorite favorite = favoriteList.get(position);
        
        // 设置商品信息
        if (favorite.getProduct() != null) {
            holder.tvName.setText(favorite.getProduct().getName());
            holder.tvPrice.setText("¥" + favorite.getProduct().getPrice());
            holder.tvDescription.setText(favorite.getProduct().getDescription());
            
            // 加载商品图片
            Glide.with(holder.itemView.getContext())
                    .load(favorite.getProduct().getImageUrl())
                    .placeholder(R.drawable.placeholder_image)
                    .into(holder.ivProduct);
        }
        
        // 设置收藏时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        String timeStr = sdf.format(new Date(favorite.getFavoriteTime()));
        holder.tvTime.setText(timeStr);
        
        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(position);
            }
        });
        
        holder.btnRemove.setOnClickListener(v -> {
            if (listener != null) {
                listener.onRemoveClick(position);
            }
        });
        
        holder.btnAddToCart.setOnClickListener(v -> {
            if (listener != null) {
                listener.onAddToCartClick(position);
            }
        });
    }
    
    @Override
    public int getItemCount() {
        return favoriteList.size();
    }
    
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
    
    /**
     * 点击监听器
     */
    public interface OnItemClickListener {
        void onItemClick(int position);
        void onRemoveClick(int position);
        void onAddToCartClick(int position);
    }
    
    /**
     * 收藏ViewHolder
     */
    static class FavoriteViewHolder extends RecyclerView.ViewHolder {
        ImageView ivProduct;
        TextView tvName;
        TextView tvPrice;
        TextView tvDescription;
        TextView tvTime;
        View btnRemove;
        View btnAddToCart;
        
        public FavoriteViewHolder(@NonNull View itemView) {
            super(itemView);
            ivProduct = itemView.findViewById(R.id.iv_product);
            tvName = itemView.findViewById(R.id.tv_name);
            tvPrice = itemView.findViewById(R.id.tv_price);
            tvDescription = itemView.findViewById(R.id.tv_description);
            tvTime = itemView.findViewById(R.id.tv_time);
            btnRemove = itemView.findViewById(R.id.btn_remove);
            btnAddToCart = itemView.findViewById(R.id.btn_add_to_cart);
        }
    }
} 